var app={
	getAllChildNodes:function(node){
		const childNodes = Array.from(node.childNodes);
		const allChildNodes = [];
		  childNodes.forEach(childNode => {
			allChildNodes.push(childNode);
			allChildNodes.push(...this.getAllChildNodes(childNode));
		  });
		return allChildNodes;
	},
	//绑定v-for
	bindVforNode:function(node,vforStr){
		// 兼容第一种 value in object
		let regex = /^\s*([^ ]+)\s+in\s+([^ ]+)$/;
		let matches= vforStr.match(regex);
		if (matches) {
			  var value = matches[1].trim();
			  var object = matches[2].trim();
			  let uuid=this.generateUUID(value);
			  node.setAttribute("data-list-obj", object);
			  node.setAttribute("data-list-key",value);
			  node.setAttribute('data-vfor-str',vforStr);
			  node.setAttribute('data-vfor-uuid',uuid);
			  
			  //绑定v-for专用子节点
			  let nodes=this.getChildNodes(node);
			  
			  let model={};
			  model.node=node;
			  model.uuid=uuid;
			  model.arryKey=object;
			  model.key=value;
			  model.childrens=nodes;
			  return model;
		}
		return null;
	},
	//深度复制某个节点
	deepCloneNode:function(node){
	 if (node.nodeType === Node.ELEMENT_NODE) {
		var clone = document.createElement(node.nodeName);
		
		// 复制节点的属性
		for (var i = 0; i < node.attributes.length; i++) {
		  var attribute = node.attributes[i];
		  clone.setAttribute(attribute.nodeName, attribute.nodeValue);
		}
		
		// 递归复制子节点
		for (var i = 0; i < node.childNodes.length; i++) {
		  var child = node.childNodes[i];
		  clone.appendChild(app.deepCloneNode(child));
		}
		
		return clone;
	  } else if (node.nodeType === Node.TEXT_NODE) {
		return document.createTextNode(node.nodeValue);
	  } else if (node.nodeType === Node.COMMENT_NODE) {
		return document.createComment(node.nodeValue);
	  } else {
		return null;
	  }
	},
	//生成uuid
	generateUUID:function(str){
		 var d = new Date().getTime();
		  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		    var r = (d + Math.random() * 16) % 16 | 0;
		    d = Math.floor(d / 16);
		    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
		  });
		  return str+'_'+uuid;
	},
	//替换{{}} 里面的值
	replacePlaceholders: function(str, data) {
	  let str_= str.replace(/{{\s*(\w+(\.\w+)*)\s*}}/g, function(match, p1) {
	     if (typeof data[p1] === 'undefined' || data[p1] === "undefined") {
	        return match;  // 返回原始的匹配内容
	      } else {
	        return data[p1];  // 返回对应的值
	      }
	  });
	  return str_;
	},
	getOutputParentheses:function(obj){
		// 使用正则表达式提取 {{ key }} 中的 key 值
		const regex = /{{\s*([^}]+)\s*}}/g;
		const matches = obj.match(regex);
		if (matches) {
			const values = matches.map(match => match.match(/{{\s*([^}]+)\s*}}/)[1].trim());
			return values;
		}
		return null;
	},
	//获取节点下子节点
	getChildNodes:function(node){
		const childNodes = Array.from(node.childNodes);
		return childNodes;
	},
	getHandler:function(callback,key,model={}){
		const handler = {
		  set: function(target, key, value) {
			// 判断是修改操作还是新增操作
			const action = target.hasOwnProperty(key) ? 'update' : 'add';
		    // 监听数组元素的修改
		    console.log(`修改了下标为 ${key} 的元素，新值为 ${value}`);
		    let obj= Reflect.set(target, key, value);
			callback(model,target,key,action);
			return obj;
		  },
		  deleteProperty: function(target, key) {
		    // 监听数组元素的删除
		    console.log(`删除了下标为 ${key} 的元素`);
		    let obj= Reflect.deleteProperty(target, key);
			//callback(model,target,key,'delete');
			return obj;
		  },
		  apply: function(target, thisArg, args) {
		    // 监听数组的方法调用（如push、pop等），用以获取新增元素下标
		    if (args[0] === 'push') {
		      const newElements = args[1];
		      console.log(`新增了 ${newElements.length} 个元素到数组，起始下标为 ${target.length}`);
		    }
			console.log('类型:'+args[0])
		    let obj=Reflect.apply(target, thisArg, args);
			//callback(model,target,args,'apply');
			return obj;
		  }
		};
		return handler;
	},
	getProxyArray:function(callback,key,arr,model={}){
		let handler=this.getHandler(callback,key,model);
		const proxyArray = new Proxy(arr, handler);
		return proxyArray;
	},
	removeChildNodes:function(node){
		while (node.firstChild) {
		  node.removeChild(node.firstChild);
		}
	},
}